package com.cskaoyan.controller;

import com.cskaoyan.annotation.LogAnnotation;
import com.cskaoyan.bean.*;
import com.cskaoyan.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Enzo Cotter on 2020/10/1.
 */
@RestController
@RequestMapping("admin/")
public class SystemController {

    @Autowired
    AdminService adminService;

    @RequestMapping("admin/list")
    public BaseRespVo adminList(String username, Integer page, Integer limit, String sort, String order){
        ListBean listBean = adminService.queryAdminListBean(username,page,limit,sort,order);
        return BaseRespVo.ok(listBean);
    }

    @RequestMapping("role/options")
    public BaseRespVo options(){
        List<RoleList> roleList = adminService.queryRole();
        return BaseRespVo.ok(roleList);
    }

    @RequestMapping("role/list")
    public BaseRespVo roleList(String name, Integer page, Integer limit, String sort, String order){
        ListBean listBean = adminService.queryRoleByInfo(name,page,limit,sort,order);
        return BaseRespVo.ok(listBean);
    }

    @RequestMapping("role/create")
    public BaseRespVo roleCreate(@RequestBody Role role){
        int i = adminService.addRoleByName(role);
        if(i == 1){
            return BaseRespVo.ok();
        }else {
            return BaseRespVo.fail();
        }
    }

    @RequestMapping("role/update")
    public BaseRespVo roleUpdate(@RequestBody Role role){
        Date date = new Date();
        int i = adminService.updateRole(role,date);
        if(i == 1){
            return BaseRespVo.ok();
        }else {
            return BaseRespVo.fail();
        }
    }
    @RequestMapping("role/delete")
    public BaseRespVo roleDelete(@RequestBody Role role){
        Integer id = role.getId();
        int i = adminService.deleteRole(id);
        if(i == 1){
            return BaseRespVo.ok();
        }else {
            return BaseRespVo.fail();
        }
    }

    @PostMapping("role/permissions")
    public BaseRespVo updatePermissionsByRoleId(@RequestBody PermissionUpdateBean_empower permission) {
        BaseRespVo baseRespVo = new BaseRespVo();
        adminService.updatePermissionsByRoleId(permission);
        baseRespVo.setErrno(0);
        baseRespVo.setMsg("成功");
        return baseRespVo;
    }

    @GetMapping("role/permissions")
    public BaseRespVo getPermissionsByRoleId(String roleId) {
        BaseRespVo baseRespVo = new BaseRespVo();
        Map<String, Object> result = adminService.getPermissions(roleId);
        baseRespVo.setErrno(0);
        baseRespVo.setMsg("成功");
        baseRespVo.setData(result);
        return baseRespVo;

    }
    @LogAnnotation(module = "创建管理员")
    @RequestMapping("admin/create")
    public BaseRespVo create(@RequestBody Admin admin){
        int i = adminService.addAdmin(admin);
        if(i == 1){
            return BaseRespVo.ok();
        }else {
            return BaseRespVo.fail();
        }
    }
    @LogAnnotation(module = "编辑管理员")
    @RequestMapping("admin/update")
    public BaseRespVo adminUpdate(@RequestBody Admin admin){
        Date date = new Date();
        int i = adminService.updateAdmin(admin,date);
        if(i == 1){
            return BaseRespVo.ok();
        }else {
            return BaseRespVo.fail();
        }
    }
    @LogAnnotation(module = "删除管理员")
    @RequestMapping("admin/delete")
    public BaseRespVo adminDelete(@RequestBody Admin admin){
        Integer id = admin.getId();
        int i = adminService.deleteAdmin(id);
        if(i == 1){
            return BaseRespVo.ok();
        }else {
            return BaseRespVo.fail();
        }
    }

    @RequestMapping("log/list")
    public BaseRespVo logList(String name, Integer page, Integer limit, String sort, String order){
        ListBean listBean = adminService.queryLogListBean(name,page,limit,sort,order);
        return BaseRespVo.ok(listBean);
    }

    @RequestMapping("storage/list")
    public BaseRespVo storageList(String name, String key, Integer page, Integer limit, String sort, String order){
        ListBean listBean = adminService.queryStorage(name,key,page,limit,sort,order);
        return BaseRespVo.ok(listBean);
    }


    @RequestMapping("storage/update")
    public BaseRespVo storageUpdate( @RequestBody Storage storage){
        Date date = new Date();
        Integer id = storage.getId();
        String name = storage.getName();
        int i = adminService.updateStorageName(id,name,date);
        if(i == 1){
            return BaseRespVo.ok();
        }else {
            return BaseRespVo.fail();
        }
    }
    @RequestMapping("storage/delete")
    public BaseRespVo storageDelete (@RequestBody Storage storage){
        Integer id = storage.getId();
        int i = adminService.deleteStorageName(id);
        if(i == 1){
            return BaseRespVo.ok();
        }else {
            return BaseRespVo.fail();
        }
    }

}
